home *** CD-ROM | disk | FTP | other *** search
/ The Essential Home & Business Collection / The Essential Home & Business Collection.iso / 27 / 3 / 5 / HP22D5.ZIP / EXTERN / DOS.C < prev    next >
Text File  |  1991-04-16  |  5KB  |  320 lines

  1. /*
  2. ** DOS.C -    This extension performs some commonly used DOS functions.
  3. **        It does not, however, perform any critical error checking
  4. **        like drive door open, disk write protected, etc...
  5. **
  6. **        The low level DOS functions are handled in assembly language
  7. **        IO.ASM.
  8. */
  9. #include <stdarg.h>
  10. #include <dos.h>
  11. #include <ctype.h>
  12.  
  13. #include "extern.h"
  14. #include "io.h"
  15.  
  16. #define COPY_BUF_SIZE            4000
  17.  
  18. union REGS near regs;
  19. struct SREGS near sregs;
  20.  
  21. /* declare this to avoid C startup references */
  22. _maperror() {}
  23.  
  24. pascal near doscmd(func,name)
  25.  
  26. STRING name;
  27. BYTE func;
  28.  
  29. {
  30.     regs.h.ah = func;
  31.     sregs.ds = FP_SEG(name);
  32.     regs.x.dx = FP_OFF(name);
  33.     intdosx(®s,®s,&sregs);
  34.     return(regs.x.cflag);
  35. }
  36.  
  37. /* returns TRUE is error, FALSE otherwise */
  38. pascal near md(name)
  39.  
  40. STRING name;
  41.  
  42. {
  43.     return(doscmd(0x39,name));
  44. }
  45.  
  46. /* returns TRUE if error, FALSE otherwise */
  47. pascal near rd(name)
  48.  
  49. STRING name;
  50.  
  51. {
  52.     return(doscmd(0x3a,name));
  53. }
  54.  
  55. /* returns TRUE if error, FALSE otherwise.  drive is a letter, like 'A' */
  56. pascal near change_disk(disk)
  57.  
  58. int disk;
  59.  
  60. {
  61.     regs.h.dl = toupper(disk) - 'A';
  62.     regs.h.ah = 0xe;
  63.     intdos(®s,®s);
  64.     return(regs.x.cflag);
  65. }
  66.  
  67. /* returns TRUE if error, FALSE otherwise */
  68. pascal near cd(dir)
  69.  
  70. STRING dir;
  71.  
  72. {
  73.     int RC;
  74.     int drive;
  75.  
  76.     /* use DOS to change the directory... */
  77.     RC = doscmd(0x3b,dir);
  78.  
  79.     /* return if wasn't able to */
  80.     if (RC) return(RC);
  81.  
  82.     /* change the logged drive, if there is a drive specifier */
  83.     return(dir[1] == ':' ? change_disk(dir[0]) : FALSE);
  84. }
  85.  
  86. pascal near rename(newname,oldname)
  87.  
  88. STRING newname,oldname;
  89.  
  90. {
  91.     regs.h.ah = 0x56;
  92.  
  93.     regs.x.dx = FP_OFF(oldname);
  94.     sregs.ds = FP_SEG(oldname);
  95.  
  96.     regs.x.di = FP_OFF(newname);
  97.     sregs.es = FP_SEG(newname);
  98.  
  99.     intdosx(®s,®s,&sregs);
  100.     return(regs.x.cflag);
  101. }
  102.  
  103. pascal near ReturnBoolean(bool)
  104.  
  105. BOOLEAN bool;
  106.  
  107. {
  108.     ReturnValue(stoh(bool ? "ERROR" : "OK"));
  109. }
  110.  
  111. MakeDir(int NumArgs,...)
  112.  
  113. {
  114.     va_list args;
  115.     HANDLE hdl;
  116.  
  117.     va_start(args,NumArgs);
  118.  
  119.     if (NumArgs != 1) return(STOP);
  120.  
  121.     hdl = va_arg(args,HANDLE);
  122.  
  123.     ReturnBoolean(md(htos(hdl)));
  124.  
  125.     return(STOP);
  126. }
  127.  
  128. RemoveDir(int NumArgs,...)
  129.  
  130. {
  131.     va_list args;
  132.     HANDLE hdl;
  133.  
  134.     va_start(args,NumArgs);
  135.  
  136.     if (NumArgs != 1) return(STOP);
  137.  
  138.     hdl = va_arg(args,HANDLE);
  139.  
  140.     ReturnBoolean(rd(htos(hdl)));
  141.  
  142.     return(STOP);
  143. }
  144.  
  145. MyChangeDir(int NumArgs,...)
  146.  
  147. {
  148.     va_list args;
  149.     HANDLE hdl;
  150.  
  151.     va_start(args,NumArgs);
  152.  
  153.     if (NumArgs != 1) return(STOP);
  154.  
  155.     hdl = va_arg(args,HANDLE);
  156.  
  157.     ReturnBoolean(cd(htos(hdl)));
  158.  
  159.     return(STOP);
  160. }
  161.  
  162. DeleteFile(int NumArgs,...)
  163.  
  164. {
  165.     va_list args;
  166.     HANDLE hdl;
  167.  
  168.     va_start(args,NumArgs);
  169.  
  170.     if (NumArgs != 1) return(STOP);
  171.  
  172.     hdl = va_arg(args,HANDLE);
  173.  
  174.     ReturnBoolean(doscmd(0x41,htos(hdl)));
  175.  
  176.     return(STOP);
  177. }
  178.  
  179. ChangeDisk(int NumArgs,...)
  180.  
  181. {
  182.     va_list args;
  183.     HANDLE hdl;
  184.  
  185.     va_start(args,NumArgs);
  186.  
  187.     if (NumArgs != 1) return(STOP);
  188.  
  189.     hdl = va_arg(args,HANDLE);
  190.  
  191.     ReturnBoolean(change_disk(((PTR)deref(hdl))[0]));
  192.  
  193.     return(STOP);
  194. }
  195.  
  196. RenameFile(int NumArgs,...)
  197.  
  198. {
  199.     va_list args;
  200.     HANDLE hSource,hDest;
  201.  
  202.     va_start(args,NumArgs);
  203.  
  204.     if (NumArgs != 2) return(STOP);
  205.  
  206.     hSource = va_arg(args,HANDLE);
  207.     hDest = va_arg(args,HANDLE);
  208.  
  209.     ReturnBoolean(rename(deref(hDest),deref(hSource)));
  210.  
  211.     return(STOP);
  212. }
  213.  
  214. CopyFile(int NumArgs,...)
  215.  
  216. {
  217.     va_list args;
  218.     HANDLE hSourceName,hDestName;
  219.     HANDLE hdl = NULL;
  220.     int source = -1,dest = -1;
  221.     int NumBytes;
  222.     union REGS regs;
  223.  
  224.     va_start(args,NumArgs);
  225.  
  226.     if (NumArgs != 2) return(STOP);
  227.  
  228.     hSourceName = va_arg(args,HANDLE);
  229.     hDestName = va_arg(args,HANDLE);
  230.  
  231.     if (!(hdl = NewHandle(COPY_BUF_SIZE))) {
  232.         ReturnValue(stoh("NOT ENOUGH MEMORY"));
  233.         goto ret;
  234.     }
  235.  
  236.     source = open(deref(hSourceName),READ);
  237.  
  238.     if (source < 0) {
  239.         ReturnValue(stoh("CANNOT OPEN SOURCE"));
  240.         goto ret;
  241.     }
  242.  
  243.     dest = create(deref(hDestName));
  244.  
  245.     if (dest < 0) {
  246.         ReturnValue(stoh("CANNOT OPEN DESTINATION"));
  247.         goto ret;
  248.     }
  249.  
  250.     do {
  251.         NumBytes = read(source,deref(hdl),COPY_BUF_SIZE);
  252.         if (NumBytes > 0) write(dest,deref(hdl),NumBytes);
  253.     } while (NumBytes > 0);
  254.  
  255.     regs.h.ah = 0x57;
  256.     regs.h.al = 0;
  257.     regs.x.bx = source;
  258.     intdos(®s,®s);
  259.     regs.h.ah = 0x57;
  260.     regs.h.al = 1;
  261.     regs.x.bx = dest;
  262.     intdos(®s,®s);
  263.  
  264.     ReturnValue(stoh("OK"));
  265.  
  266. ret:
  267.     if (source > 0) close(source);
  268.     if (dest > 0) close(dest);
  269.     FreeHandle(hdl);
  270.     return(STOP);
  271. }
  272.  
  273. POOL pascal Pool[] = {
  274.  
  275.     {    "CopyFile",
  276.         CopyFile,
  277.         0,
  278.         FUNCTION},
  279.  
  280.     {    "RenameFile",
  281.         RenameFile,
  282.         0,
  283.         FUNCTION},
  284.  
  285.     {    "ChangeDisk",
  286.         ChangeDisk,
  287.         0,
  288.         FUNCTION},
  289.  
  290.     {    "DeleteFile",
  291.         DeleteFile,
  292.         0,
  293.         FUNCTION},
  294.  
  295.  
  296.     {    "ChangeDir",
  297.         MyChangeDir,
  298.         0,
  299.         FUNCTION},
  300.  
  301.     {    "RenameFile",
  302.         RenameFile,
  303.         0,
  304.         FUNCTION},
  305.  
  306.     {    "RemoveDir",
  307.         RemoveDir,
  308.         0,
  309.         FUNCTION},
  310.  
  311.     {    "MakeDir",
  312.         MakeDir,
  313.         0,
  314.         FUNCTION},
  315.  
  316.     {    NULL,
  317.         NULL,
  318.         0,
  319.         0}    };
  320.